<template>
  <div class="question-list-container">
    <el-card class="question-list-card">
      <template #header>
        <div class="card-header">
          <h2>试题列表</h2>
        </div>
      </template>
      
      <!-- 筛选条件 -->
      <div class="filter-container">
        <el-form :inline="true" :model="filters" class="filter-form">
          <el-form-item label="题型">
            <el-select v-model="filters.type" placeholder="全部题型" clearable @change="handleFilterChange">
              <el-option label="选择题" value="MULTIPLE_CHOICE" />
              <el-option label="判断题" value="TRUE_FALSE" />
              <el-option label="填空题" value="FILL_BLANK" />
              <el-option label="简答题" value="SHORT_ANSWER" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="难度">
            <el-select v-model="filters.difficulty" placeholder="全部难度" clearable @change="handleFilterChange">
              <el-option label="难度 1" :value="1" />
              <el-option label="难度 2" :value="2" />
              <el-option label="难度 3" :value="3" />
              <el-option label="难度 4" :value="4" />
              <el-option label="难度 5" :value="5" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="知识点">
            <el-input v-model="filters.knowledgePoint" placeholder="知识点关键词" @keyup.enter="handleFilterChange" clearable />
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="handleFilterChange">查询</el-button>
            <el-button @click="resetFilters">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="questions"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="60" />
        
        <el-table-column prop="type" label="题型" width="100">
          <template #default="scope">
            <el-tag :type="getQuestionTypeTag(scope.row.type)">
              {{ formatQuestionType(scope.row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column prop="content" label="题目内容" min-width="250">
          <template #default="scope">
            <div class="question-content">
              {{ truncateText(scope.row.content, 50) }}
            </div>
          </template>
        </el-table-column>
        
        <el-table-column prop="difficulty" label="难度" width="100">
          <template #default="scope">
            <el-rate
              v-model="scope.row.difficulty"
              disabled
              text-color="#ff9900"
              score-template="{value}"
            />
          </template>
        </el-table-column>
        
        <el-table-column prop="score" label="分值" width="80" />
        
        <el-table-column prop="knowledgePoint" label="知识点" width="120">
          <template #default="scope">
            <el-tag type="info" size="small" v-if="scope.row.knowledgePoint">
              {{ scope.row.knowledgePoint }}
            </el-tag>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <router-link :to="`/questions/${scope.row.id}`">
              <el-button size="small" type="primary" plain>查看</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-if="total > 0"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :current-page="currentPage"
          @current-change="handlePageChange"
          @size-change="handleSizeChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'

export default {
  name: 'QuestionList',
  setup() {
    const store = useStore()
    
    // 试题列表相关数据
    const questions = computed(() => store.getters.allQuestions)
    const total = computed(() => store.state.questions.total)
    const loading = computed(() => store.state.isLoading)
    const currentPage = ref(1)
    const pageSize = ref(10)
    
    // 筛选条件
    const storeFilters = computed(() => store.getters.questionFilters)
    const filters = reactive({
      type: '',
      difficulty: null,
      knowledgePoint: ''
    })
    
    // 初始化
    onMounted(() => {
      // 从store中恢复上次的筛选条件
      if (storeFilters.value) {
        filters.type = storeFilters.value.type || ''
        filters.difficulty = storeFilters.value.difficulty || null
        filters.knowledgePoint = storeFilters.value.knowledgePoint || ''
      }
      
      fetchQuestions()
    })
    
    // 获取试题列表
    const fetchQuestions = async () => {
      try {
        const result = await store.dispatch('fetchQuestions', {
          page: currentPage.value,
          size: pageSize.value,
          type: filters.type,
          difficulty: filters.difficulty,
          knowledgePoint: filters.knowledgePoint
        })
        
        if (!result || !result.success) {
          ElMessage.error(result?.message || '获取试题列表失败')
        }
      } catch (error) {
        console.error('获取试题列表失败:', error)
        ElMessage.error('获取试题列表失败，请重试')
      }
    }
    
    // 分页处理
    const handlePageChange = (page) => {
      currentPage.value = page
      fetchQuestions()
    }
    
    // 每页条数改变
    const handleSizeChange = (size) => {
      pageSize.value = size
      currentPage.value = 1
      fetchQuestions()
    }
    
    // 筛选条件改变
    const handleFilterChange = () => {
      currentPage.value = 1
      fetchQuestions()
    }
    
    // 重置筛选条件
    const resetFilters = () => {
      filters.type = ''
      filters.difficulty = null
      filters.knowledgePoint = ''
      currentPage.value = 1
      fetchQuestions()
    }
    
    // 格式化试题类型
    const formatQuestionType = (type) => {
      const typeMap = {
        'MULTIPLE_CHOICE': '选择题',
        'TRUE_FALSE': '判断题',
        'FILL_BLANK': '填空题',
        'SHORT_ANSWER': '简答题'
      }
      return typeMap[type] || type
    }
    
    // 获取试题类型标签样式
    const getQuestionTypeTag = (type) => {
      const tagMap = {
        'MULTIPLE_CHOICE': 'primary',
        'TRUE_FALSE': 'success',
        'FILL_BLANK': 'warning',
        'SHORT_ANSWER': 'info'
      }
      return tagMap[type] || ''
    }
    
    // 文本截断
    const truncateText = (text, length) => {
      if (!text) return ''
      if (text.length <= length) return text
      return text.substring(0, length) + '...'
    }
    
    return {
      questions,
      total,
      loading,
      currentPage,
      pageSize,
      filters,
      handlePageChange,
      handleSizeChange,
      handleFilterChange,
      resetFilters,
      formatQuestionType,
      getQuestionTypeTag,
      truncateText
    }
  }
}
</script>

<style scoped>
.question-list-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.question-list-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 1.5rem;
  color: #303133;
}

.filter-container {
  margin-bottom: 20px;
}

.filter-form {
  display: flex;
  flex-wrap: wrap;
}

.question-content {
  max-height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style> 